// pages/home/sholis/sholis.js

var app = getApp()
var event = require('../../../utils/event')
var config = require('../../../config.js')


Page({
  data: {
    tabTxt: ['区域', '面积'],//分类
    hidetop: true,
    tab: [true, true, true],
    pinpaiList: config.constant.areaList,
    pinpai_id: -1,//区域
    pinpai_txt: '',
    jiageList: app.globalData.sizeList,
    jiage_id: -1,//面积
    jiage_txt: '',
    xiaoliangList: [{ 'id': '0', 'title': '农林牧副渔' }, { 'id': '1', 'title': '互联网IT' }, { 'id': '2', 'title': '工业' }, { 'id': '3', 'title': '商超' }],
    xiaoliang_id: -1,//销量
    xiaoliang_txt: '',
    objHostName: `${config.service.objHostName}`,
    list: [],
    pageSize: 10,
    ye: 1,
    hideBottomLabel: true,
    overHeight: false,

    indexData: {},
    counts: 10,
    start: 0
  },

  onLoad: function () {
    this.getDataList();
    if (undefined == this.data.pinpaiList || this.data.pinpaiList.length == 0) {
      this.setData({
        pinpaiList: app.globalData.areaList
      })
    }
  },
  onShow: function () {
    console.log('index-onShow')
    var that = this

    return;
    wx.getSetting({
      success(res) {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API

                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          console.log('调用wx.getLocation的API');
          wx.getLocation({
            success: function (res) {
              console.log('res->' + res)



            },
            fail: function (err) {
              console.log('err->' + err)
            }
          })
        }
        else {
          //调用wx.getLocation的API
          console.log('调用wx.getLocation的API1');
        }

      }
    })


    // app.getpol(
    //     function (currentCity) {
    //         that.setData({
    //             'head.currentCity': currentCity
    //         })
    //     }
    // )
  },

  onReachBottom: function () {
    var that = this
    // 
    var ye = that.data.ye;
    ye += 1;
    that.setData({
      ye: ye
    });
    that.getDataList();
  },

  getLoc: function () {
    wx.getLocation({
      success: function (res) {

      },
      fail: function (err) {

      },
      complete: function (comp) {

      }
    })
  },
  // 选项卡
  filterTab: function (e) {
    var overHeight = this.data.overHeight;
    var data = [true, true, true], index = e.currentTarget.dataset.index;
    var newFlag = !this.data.tab[index];

    if (overHeight == false) {
      if (newFlag) {
        this.setData({
          hidetop: true
        });
      }
      else {
        this.setData({
          hidetop: false
        });
      }
    }
    else {

    }


    data[index] = newFlag;
    this.setData({
      tab: data
    })

    console.log(this.data.tab)

  },

  //筛选项点击操作
  filter: function (e) {
    var overHeight = this.data.overHeight;
    if (overHeight) {
      this.setData({
        hidetop: false
      });
    }
    else {
      this.setData({
        hidetop: true
      });
    }

    var self = this
    var id = e.currentTarget.dataset.id
    var txt = e.currentTarget.dataset.txt
    console.log('txt:' + txt);

    var tabTxt = this.data.tabTxt

    switch (e.currentTarget.dataset.index) {
      case '0':
        tabTxt[0] = txt;
        self.setData({
          pinpai_id: id,
          pinpai_txt: txt
        });
        break;
      case '1':
        tabTxt[1] = txt;
        self.setData({
          jiage_id: id,
          jiage_txt: txt
        });
        break;
      case '2':
        tabTxt[2] = txt;
        self.setData({
          xiaoliang_id: id,
          xiaoliang_txt: txt
        });

        console.log('txt:' + txt);

        break;
    }

    self.setData({
      ye: 1,
      tab: [true, true, true],
      tabTxt: tabTxt
    })
    //数据筛选
    self.getDataList();
  },

  cancelFilter: function () {
    console.log('dddd');
    var overHeight = this.data.overHeight;
    this.setData({
      ye: 1,
      tab: [true, true, true],
      hidetop: !overHeight
    })
  },

  //加载数据
  getDataList: function () {
    //调用数据接口，获取数据
    var that = this;
    var ye = that.data.ye;
    var data = {
      row: that.data.pageSize,
      ye: ye,
      area: that.data.pinpai_id,
      size: that.data.jiage_id,
      // bustyp: that.data.xiaoliang_id
    }
    console.log('data:'+JSON.stringify(data));
    wx.showNavigationBarLoading();
    wx.request({
      url: `${config.service.shopUrl}`,
      method: 'POST',
      timeout: 2000,
      data: data,
      success: function (e) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        wx.hideLoading();
        console.log('res:' + JSON.stringify(e.data.res))
        var code = e.data.code;
        if (code == 0) {
          var objs = e.data.res;

          if (objs.length < that.data.pageSize) {
            // 加载完了
            that.setData({ hideBottomLabel: false });
          }

          for (var i = 0; i < objs.length; i++) {
            var obj = objs[i];
            var img = obj['img'];
            // console.log('img:'+img);
            var imgs = img.split(",");
            // console.log(imgs);
            obj.imgs = imgs;
            obj.hideTop = ('0' == obj.top)
          }

          // ye==1 表示下拉刷新或第一次进入页面
          if (ye == 1) {
            that.setData({ list: objs });
          } else {
            that.setData({
              list: that.data.list.concat(objs)
            });
          }
        } else {
          var msg = e.data.msg;
          console.log(e.data);
          if (msg) {
            wx.showToast({
              title: msg,
            })
          }
          else {
            wx.showToast({
              title: JSON.stringify(e.data),
            })
          }

        }
      },
      fail: function (failed) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        wx.hideLoading();
        // that.stopRefreshView();
        console.log(failed);
      }
    })
  },

  onPageScroll: function (e) {
    var hidetop = this.data.hidetop;
    var tab = this.data.tab;
    console.log(tab);

    if (JSON.stringify(tab) !== JSON.stringify([true, true, true]) && hidetop == false) {
      return;
    }


    var that = this;
    console.log(e);//{scrollTop:99}
    console.log('hidetop:' + that.data.hidetop);
    if (e.scrollTop < 80) {
      if (!that.data.hidetop) {
        that.setData({
          hidetop: true
        });
      }
      that.setData({
        overHeight: false
      })
    }
    else {
      if (that.data.hidetop) {
        that.setData({
          hidetop: false
        });
      }
      that.setData({
        overHeight: true
      })
    }
  },

  onPullDownRefresh:function(){
    console.log('pull down')
    this.setData({
      ye:1
    })
    this.getDataList()
  }
})
